import { AuthorityTree, AuthorityTitle, RoleInfo } from "@/type/roleManage";
import { LazyTableService } from "@/hooks/useLazyLable";
import axios, { Response, CancelToken, authGuard } from "./axios";
import factorySingleClass from "./SingleClass";

interface RoleManageService {
    GETLIST: string;
    REMOVE: string;
    SAVE: string;
    UPDATE: string;
}

let cancel: any;
class RoleManageService extends LazyTableService {
    authority() {
        return axios.get<
            Response<{ titles: AuthorityTitle[]; resources: AuthorityTree[] }>
        >(`/usersys-auth/role/function_authority`);
    }
    @authGuard
    save(obj: RoleInfo) {
        return axios.post<any>(`/usersys-auth/role/save`, obj);
    }
    @authGuard
    getList(obj: any = {}) {
        cancel && cancel();
        return axios.post<Response<RoleInfo[]>>(
            `/usersys-auth/role/roles`,
            obj,
            {
                cancelToken: new CancelToken((c) => (cancel = c)),
            },
        );
    }
    @authGuard
    remove(id: string) {
        return axios.delete(`/usersys-auth/role/delete?roleId=${id}`);
    }
    enable(id: any, orgId: any, state: boolean) {
        return axios.put<any>(
            `usersys-auth/role/state?roleId=${id}&orgId=${orgId}&state=${state}`,
        );
    }
    disable(id: any, orgId: any, state: boolean) {
        return axios.put<any>(
            `usersys-auth/role/state?roleId=${id}&orgId=${orgId}&state=${state}`,
        );
    }
    getRoleById(id: any) {
        return axios.get<Response<RoleInfo>>(`/usersys-auth/role?roleId=${id}`);
    }
    @authGuard
    update(obj: RoleInfo) {
        return axios.put<any>(`/usersys-auth/role/update`, obj);
    }
    getRoleListByCompany(orgId: string) {
        return axios.get<Response<RoleInfo[]>>(
            `/usersys-auth/role/roles/basic?orgId=${orgId}`,
        );
    }
    getAllRolesByCompany() {
        return axios.get<Response<RoleInfo[]>>(
            `/usersys-auth/role/roles/basic`,
        );
    }
}
export default factorySingleClass(RoleManageService);

let cancel1: any;
class RoleManageAdminService extends LazyTableService {
    getList(obj: any) {
        cancel1 && cancel1();
        return axios.get(`/usersys-auth/role/roles/global?${obj}`, {
            cancelToken: new CancelToken((c) => (cancel1 = c)),
        });
    }
    state(id: any, state: boolean) {
        return axios.put<Response<any>>(
            `/usersys-auth/role/state/global?roleId=${id}&state=${state}`,
        );
    }
    remove(id: string) {
        return axios.delete(`/usersys-auth/role/delete?roleId=${id}`);
    }
}
const roleManageAdminService = factorySingleClass(RoleManageAdminService);
export { roleManageAdminService };
